#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.


mountdir=$(mktemp -d)
filepath=$mountdir/cloudstack

user_data=$filepath/userdata/user_data.txt
vm_password=$filepath/password/vm_password.txt

declare -A metadata=(
["availability-zone"]=$filepath/metadata/availability-zone.txt
["cloud-identifier"]=$filepath/metadata/cloud-identifier.txt
["instance-id"]=$filepath/metadata/instance-id.txt
["local-hostname"]=$filepath/metadata/local-hostname.txt
["local-ipv4"]=$filepath/metadata/local-ipv4.txt
["public-ipv4"]=$filepath/metadata/public-ipv4.txt
["service-offering"]=$filepath/metadata/service-offering.txt
["vm-id"]=$filepath/metadata/vm-id.txt
["public-key"]=$filepath/metadata/public-keys.txt
)
# If lable name is other than config, please change the below line as required
DefaultDisk=/dev/disk/by-label/config-2

function usage
{
    keys=${!metadata[@]}
    echo -e "USAGE: cloud-get-vm-data -options"
    echo -e "  where options include:"
    echo -e "\\t-m | --metadata [${keys// / | }] \\n\\t\\tprint vm metadata"
    echo -e "\\t-p | --password \\n\\t\\tprint vm password"
    echo -e "\\t-u | --userdata \\n\\t\\tprint vm userdata"
}

function prepare_mount
{
    if [ ! -e $mountdir ]; then
        mkdir $mountdir
        chmod 700 $mountdir
    fi

    if [ -e $DefaultDisk ]; then
        Disk=$DefaultDisk
    else
        BLOCK_DEVICE=$(blkid -t LABEL='config-2' /dev/sr? /dev/hd? /dev/sd? /dev/xvd? -o device)
        if [ -n $BLOCK_DEVICE ]; then
            Disk=$BLOCK_DEVICE
        else
            logger -t "cloud" "Unable to get the VM data: Config drive device not found"
            exit 1
        fi
    fi

    mount -r $Disk $mountdir
    if [ $? -ne 0 ]; then
        echo "Failed mounting $Disk to /mnt/configdrive"
        exit 1
    fi
}

function remove_mount
{
  umount $mountdir
}

prepare_mount

case $1 in
    -u | --userdata )   echo -n "USERDATA: "
                        filename=$user_data
                        ;;
    -m | --metadata )   shift
                        if [ "$1" != "" ]; then
                            if [ -n "${metadata[$1]}" ]; then
                                echo -n "$1: "
                                filename=${metadata[$1]}
                            else
                                usage
                                remove_mount
                                exit 1
                            fi
                        else
                            echo -e "METADATA\\n"
                            for entry in "${!metadata[@]}"
                            do
                                file=${metadata[$entry]}
                                [ -f $file ] && printf "%18s :\t%s\n" $entry "$(cat $file)"
                            done
                        fi
                        ;;
    -p | --password )   echo -n "PASSWORD: "
                        filename=$vm_password
                        ;;
    -h | --help )       usage
                        remove_mount
                        exit 0
                        ;;
    * )                 usage
                        remove_mount
                        exit 1
esac

if [ "$filename" != "" ] && [ -e $filename ]
then
    cat $filename
fi

remove_mount
exit 0
